package com.bytz.modules.cms.auditBatch.job;

import cn.hutool.core.bean.BeanUtil;
import com.bytz.common.system.vo.LoginUser;
import com.bytz.common.util.SecurityUtils;
import com.bytz.modules.cms.auditBatch.service.IAdminAuditBatchService;
import com.bytz.modules.cms.auditBatch.service.IAuditBatchProductService;
import com.bytz.modules.cms.auditBatch.service.IAuditBatchResellerProductService;
import com.bytz.modules.cms.product.entity.ProductDiscountCategoryLevelChange;
import com.bytz.modules.cms.productLine.entity.ProductLineLevelChange;
import com.bytz.modules.cms.reseller.entity.ResellerLevelChange;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import com.bytz.modules.system.service.ISysUserService;
import com.bytz.modules.system.vo.SysUserVo;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ChangeSyncJob implements Job {

    @Autowired
    private IAdminAuditBatchService<ProductLineLevelChange> lineService;

    @Autowired
    private IAdminAuditBatchService<ProductDiscountCategoryLevelChange> productCategoryService;

    @Autowired
    private IAdminAuditBatchService<ResellerLevelChange> resellerService;

    @Autowired
    private IAuditBatchProductService productService;

    @Autowired
    private IAuditBatchResellerProductService resellerProductService;

    @Autowired
    ISysUserService sysUserService;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) {

        SysUserVo syscall = sysUserService.getUserByName("syscall");
        SecurityUtils.setLoginUser(BeanUtil.copyProperties(syscall, LoginUser.class));

        try {
            lineService.syncChange();
        } catch (Exception e) {
            log.error("产品线等级折扣同步失败" + e);
        }

        try {
            productCategoryService.syncChange();
        } catch (Exception e) {
            log.error("产品优惠分类等级折扣同步失败" + e);
        }

        try {
            resellerService.syncChange();
        } catch (Exception e) {
            log.error("经销商/产品线等级同步失败" + e);
        }

        try {
            productService.syncChange(AuditBatchConstants.Type.PRODUCT);
        } catch (Exception e) {
            log.error("产品价格变更同步失败" + e);
        }

        try {
            productService.syncChange(AuditBatchConstants.Type.RESELLER_PRODUCT_OTHER_CHANGE);
        } catch (Exception e) {
            log.error("同步商品其他信息失败（交期、最大订货量、成本、上下架状态）" + e);
        }

        try {
            resellerProductService.syncChange();
        } catch (Exception e) {
            log.error("经销商独占产品变更同步失败" + e);
        }

        SecurityUtils.setLoginUser(null);
    }
}
